package com.bos.service.imp;

import com.bos.dao.IUserDao;
import com.bos.domain.Role;
import com.bos.domain.User;
import com.bos.service.IUserService;
import com.bos.utils.MD5Utils;
import com.bos.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * @author Administrator
 */

@Service
@Transactional
public class IUserServiceImp implements IUserService {

    @Autowired
    private IUserDao userDao;

    @Override
    public User login(User user) {
        String password = MD5Utils.md5(user.getPassword());
        return userDao.findByUsernameAndPassword(user.getUsername(), password);
    }

    @Override
    public void editPassword(String id, String newPwd) {
        userDao.executeUpdate("user.edictPassword",MD5Utils.md5(newPwd),id);
    }

    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }

    @Override
    public void save(User user, String[] roleIds) {
        Set roles = user.getRoles();
        for (String roleId : roleIds) {
            Role role = new Role(roleId);
            roles.add(role);
        }
        userDao.save(user);
    }

    @Override
    public void pageQuery(PageBean pageBean) {
        userDao.pageQuery(pageBean);
    }

}
